<?php
include_once "../lib/core.class.php";
include_once "../lib/coredb.class.php";
include_once "../lib/fckeditor/fckeditor.php";
global $core;


class AdminFramework extends iFramework 
{
    function AdminFramework()
    {
        iFramework::iFramework("admin_framework",
                        array(
                            "plugins_folder" => "",
                            "plugin_export_function_name" => "adminExport",
                            "default_plugin" => "newslinelist",
                            "template_dir" => "/admin/tpl",
                            "compile_dir" => "/inventory/temp/smarty/atemplates_c",
                            "config_dir" => "",
                            "cache_dir" => "",
                        ));
        $core = &getCoreInstance();
        $this->db = &new CoreDB($core->config["DB"]["dbtype"]."://".$core->config["DB"]["username"].":".$core->config["DB"]["password"]."@".$core->config["DB"]["hostspec"]."/".$core->config["DB"]["database"]);
#       $this->db->initCache($core->config["DB"]['caching']);
        $this->db->initCache($usecache = false);
        $core->setProperty("db", $this->db);
    }
    
    function doAction($arg = null)
    {
        if(null == $arg || !is_array($arg) || !count($arg))
        {
            $core = getCoreInstance();
            $arg = array($this->default_plugin);
        }
        $tag = $arg[0];
        if(isset($this->plugins[$tag]))
        {
            unset($arg[0]);
            $arg = ArrayReIndex($arg); //array_flip(array_flip($arg));
            return $this->plugins[$tag]->doAction($arg);
        }
        return false;
    }
    
    
    function CreateLeftMenu()
    {
        $menu = array();

        foreach($this->plugins as $tag => $p)
        {
            $z = array_flip($p->getProperty());
            if(isset($z["MenuBlockTitle"]))
            {
                $caption = $p->getProperty("MenuTitle");
                $url = $p->getURL();
                
                $mitem = array( array("title" => $caption, "URL" => "?".$url) );
                if(count($p->childs))
                {
                    foreach ($p->childs as $stag => $sp)
                    {
                        $z = array_flip($p->getProperty());
                        if(isset($z["MenuTitle"]))
                        {
                            $mitem[] = array("title" => $sp->getProperty("MenuTitle"), "URL" => "?".$sp->getURL());
                        }
                    }
                }
                $menu[$caption] = array("caption" => $caption, "menublockcaption" => $p->getProperty("MenuBlockTitle"), "color" => $p->color, "items" => $mitem);
//              debug($menu);
            }
        }
        
        return $menu;
    }
    

    function render($arg = null)
    {
        $core = &getCoreInstance();
        if(null == $arg || !is_array($arg) || !count($arg))
        {
            $arg = array($this->default_plugin);
        }
        
        $menu = $this->CreateLeftMenu();
//debug($menu, get_class($this));
        $this->smarty->assign("func_list", $menu);
        $this->smarty->assign("prj_title", $core->config["system"]["prj_title"]);
        $this->smarty->assign("func_list1", "<b>menu</b>");

        
        $tag = $arg[0];
        $HTML = "Empty page";
        if(isset($this->plugins[$tag]))
        {
            unset($arg[0]);
            $arg = ArrayReIndex($arg);
            $HTML = $this->plugins[$tag]->Render($arg);
        }
        
        $plugin = $core->getPathArguments();
        $this->smarty->assign("parent_page", isset($this->plugins[$plugin[0]]) ? $this->plugins[$plugin[0]]->getProperty("MenuTitle") : $this->plugins[$this->default_plugin]->getProperty("MenuTitle"));

        $this->smarty->assign("username", $_SESSION["username"]);
        
        $this->smarty->assign("content", $HTML);
        $page = "index.tpl.php";
        if(1 == $_GET["showCloseButton"] || "Yes" == $_GET["showCloseButton"])
        {
            $this->smarty->assign("showCloseButton", 1);
        }
        if($_GET["doPrint"]=="doPrint")
        {
            $page = "index_light.tpl.php";
        }
                
                if($_GET["doPrint"]=="labels")
        {
            $page = "index_none.tpl.php";
        }
                
        //added by Gendich ----- start
        $db=$GLOBALS[db];

                if (isset($_SESSION[del_tmp]) && !($_GET[path]=="orderslist,productlist" && $_GET[page]=="add"))
                {
                        $db->exec("delete from catalog where is_temp='1'  and is_category='N'");
                        unset($_SESSION[del_tmp]);
                }
        // added by Gendich ----- end
        if($core->config["DB"]['debug']){
                $db->showCache();
        }

        return $this->smarty->fetch($page);
        
    }

};

?>